class Solution {
public:
    typedef long long int ll;
    int minOperations(vector<int>& nums, int x) 
    {
        int n=nums.size();
        if(nums[0]>x&&nums[n-1]>x) return -1;

        if(nums[0]==x||nums[n-1]==x)  return 1;
    
        ll sum=0;
        for(auto&k:nums) sum+=k;
        if(sum<x) return -1;
        if(sum==x) return n;

        //滑动窗口
        int k=sum-x;
        int l=0,r=0,ans=-1;
        sum=0;
        while(r<n)
        {
            sum+=nums[r];
            l++;
        }
        if(sum==k)
        {
            ans=max(ans,r-l+1);
        }
        r++;
        return ans==-1?-1:n-ans;

    }
};